package com.freewater.cargotracker.domain.service;

import com.freewater.cargotracker.domain.model.cargo.Cargo;
import com.freewater.cargotracker.domain.model.voyage.Voyage;
import com.freewater.cargotracker.domain.model.voyage.VoyageRepository;

public class DefaultOverBookingPolicy implements OverBookingPolicy {

    private final VoyageRepository voyageRepository;

    public DefaultOverBookingPolicy(VoyageRepository voyageRepository) {
        this.voyageRepository = voyageRepository;
    }

    @Override
    public boolean isAllowed(Cargo cargo, Voyage voyage) {
        final double bookedCargoSize = voyageRepository.findBookedCargoSize(voyage.getId());
        return (cargo.getSize() + bookedCargoSize) <= voyage.getCapacity() * 1.1;
    }

}
